const BASE_URL = "http://192.168.1.27:8063" //地址

// 请求
const httpRequest = (url, data, method) => {
	uni.showLoading({
		title: '加载中..'
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			data: data,
			method: method,
			header:{
				"Authorization":'Bearer '+ uni.getStorageSync("token") || ""
			},
			success: (res) => {
				if (res.data.code == 200) {
					resolve(res.data)
					uni.hideLoading();
				} else {
					uni.hideLoading();
					uni.showModal({
						title: '提示',
						showCancel: false,
						content: res.data.msg,
					})
				}
			},
			fail(error) {
				uni.hideLoading();
				uni.showToast({
					title:'服务异常!',
					icon:'error'
				})
				reject(error)
			},
			complete(e) {}
		});
	})
}

// 上传图片的请求
const uploadImage = (url, data) => {
	uni.showLoading({
		title: '加载中'
	});
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: BASE_URL + url,
			filePath: data,
			name: 'file',
			formData: {},
			success: (uploadFileRes) => {
				let parms = uploadFileRes.data
				resolve(parms)
				uni.hideLoading();
			},
			fail: () => {
				uni.hideLoading();
				uni.showToast({
					title: '图片上传失败',
					icon: 'none'
				})
			}
		});
	})
}


export default {
	httpRequest,
	uploadImage,
	BASE_URL
}